﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Kinect.Toolbox.Record;
using Microsoft.Kinect;
using Kinect.Toolbox;
using System.IO;
using Microsoft.Win32;
using System.Threading;
using System.Windows.Threading;
using System.Timers;
using System.ComponentModel;

namespace TigerControl
{
    /// <summary>
    /// Interaction logic for EditWindow.xaml
    /// </summary>
    public partial class EditWindow : Window
    {

        KinectReplay replay;
        ColorStreamManager colorManager = new ColorStreamManager();
        int frameNumber = 1;

        public EditWindow()
        {
            InitializeComponent();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            if (Application.Current.MainWindow.IsEnabled == false)
            {
                Application.Current.MainWindow.IsEnabled = true;
            }
        }

        private void LoadBtn_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog replayFileDialog = new OpenFileDialog { Title = "Select filename", Filter = "Replay files|*.replay" };
            if (replayFileDialog.ShowDialog() == true)
            {
                Stream recordStream = File.OpenRead(replayFileDialog.FileName);
                replay = new KinectReplay(recordStream);     
                replay.ColorImageFrameReady += replay_ColorImageFrameReady;
                replay.Start();
                frameNumber = 1;
            }
        }

        private void replay_ColorImageFrameReady(object sender, ReplayColorImageFrameReadyEventArgs e)
        {
            byte[] data = new byte[10000];
            
            e.ColorImageFrame.CopyPixelDataTo(data);
            
            ColorImagePaintOnPanel(data);
        }

        private void ColorImagePaintOnPanel(byte[] data)
        {
            MemoryStream ms = new MemoryStream(data);
            BitmapImage image = new BitmapImage();
            image.BeginInit();
            image.StreamSource = ms;
            image.EndInit();
            Image i = new Image();
            i.Height = 120;
            i.Width = 160;
            i.Source = image;
            StackPanel panel = new StackPanel();
            panel.Children.Add(i);
            Label l = new Label();
            l.Content = frameNumber;
            panel.Children.Add(l);
            EditVideoPanel.Items.Add(panel);
   
            FrameCount.Content = "Video Frame Count: " + frameNumber;
            frameNumber++;
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            StackPanel panel = (StackPanel)EditVideoPanel.SelectedItem;
            Label label = (Label)panel.Children[1];
            string n = label.Content.ToString();
        }
    }
}
